package app.goods.info.views.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import app.goods.info.constants.GlobalCache;
import app.goods.info.services.LoginTokenService;
import app.goods.info.views.LoginDialog;
import app.goods.info.views.MainFrame;
import cn.hutool.json.JSONUtil;

public class ButtonActionPerformListenner implements ActionListener {
	private static final Logger LOGGER = LoggerFactory.getLogger(ButtonActionPerformListenner.class);
	private LoginDialog loginDialog;

	public ButtonActionPerformListenner(LoginDialog loginDialog) {
		super();
		this.loginDialog = loginDialog;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String command = e.getActionCommand();
		switch (command) {
		case "OK": {
			int loginType = loginDialog.getLoginType();
			// Login by SMS
			if (loginType == 0) {
				String mobileNo = loginDialog.getMobileTextField().getText().trim();
				String verifyCode = loginDialog.getVerifyCodeTextField().getText().trim();
				if (StringUtils.isNotEmpty(mobileNo) && StringUtils.isNotEmpty(verifyCode)) {
					LoginTokenService loginTokenService = new LoginTokenService();
					String reponseBody = loginTokenService.getLoginBySmsResponse(loginTokenService
							.buildLoginBySmsUserInfo(mobileNo, loginDialog.getCaptchaToken(), verifyCode));
					LOGGER.info(reponseBody);
					if ("0".equals(JSONUtil.parseObj(reponseBody).getStr("code"))) {
						String token = JSONUtil.parseObj(reponseBody).getJSONObject("result").getStr("token");
						if (StringUtils.isNotEmpty(token)) {
							GlobalCache.getInstance().setLoginToken(token);
							GlobalCache.getInstance().initAllCache();
							MainFrame window = new MainFrame();
							window.show();
							loginDialog.dispose();
						}
					} else {
						JOptionPane.showMessageDialog(loginDialog, "登录失败，稍后再试", "", JOptionPane.INFORMATION_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(loginDialog, "输入正确的用户名和密码", "", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			// Login by user name
			if (loginType == 1) {
				String username = loginDialog.getUserName().getText().trim();
				String passwd = new String(loginDialog.getPasswordField().getPassword());
				LOGGER.debug("Password is: {} ", passwd);
				if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(passwd)) {
					LoginTokenService loginTokenService = new LoginTokenService();
					String reponseBody = loginTokenService
							.getLoginResponse(loginTokenService.buildLoginUserInfo(username, passwd));
					LOGGER.info(reponseBody);
					if ("0".equals(JSONUtil.parseObj(reponseBody).getStr("code"))) {
						String token = JSONUtil.parseObj(reponseBody).getJSONObject("result").getStr("token");
						if (StringUtils.isNotEmpty(token)) {
							GlobalCache.getInstance().setLoginToken(token);
							GlobalCache.getInstance().initAllCache();
							MainFrame window = new MainFrame();
							window.show();
							loginDialog.dispose();
						}
					} else {
						JOptionPane.showMessageDialog(loginDialog, "登录失败，稍后再试", "", JOptionPane.INFORMATION_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(loginDialog, "输入正确的用户名和密码", "", JOptionPane.INFORMATION_MESSAGE);
				}

			}
			break;
		}
		case "Cancel": {
			LOGGER.info("Push Cancel button, close the window.");
			System.out.print("=============");
			loginDialog.dispose();
			break;
		}
		}
	}

}
